package com.longjianghuasheng.logistics.business.address

import android.os.Bundle
import androidx.fragment.app.setFragmentResult
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseTitleFragment
import com.longjianghuasheng.logistics.business.address.adapter.AddressAdapter
import com.longjianghuasheng.logistics.business.inquiry.InquiryFragment
import com.longjianghuasheng.logistics.databinding.FragmentAddressListBinding
import com.longjianghuasheng.logistics.ext.deleteDialog
import com.longjianghuasheng.logistics.ext.initLinerDecoration
import com.longjianghuasheng.logistics.ext.observer

class AddressListFragment : BaseTitleFragment<AddressViewModel, FragmentAddressListBinding>() {
    private val addressAdapter by lazy {
        AddressAdapter()
    }

    override fun layoutId(): Int = R.layout.fragment_address_list
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        initTitleBar("地址管理")
        mDatabind.initRecycler()
    }

    private fun FragmentAddressListBinding.initRecycler() {
        addressAdapter.run {

            rvAddress.initLinerDecoration(adapter = this).setHasFixedSize(false)
            onCheckListener = { i: Int, b: Boolean ->
                mViewModel.address_default(i)
            }

            setOnItemClickListener { adapter, view, position ->


                val type = arguments?.getString("type") ?: ""



                if (type.isBlank()) {

                    navigateAddress(
                        getItem(position).id,
                        getItem(position).province.toString(),
                        getItem(position).city.toString(),
                        getItem(position).area.toString()
                    )

                    return@setOnItemClickListener

                }
                setFragmentResult(type, Bundle().apply {
                    putParcelable("address", data[position])

                })
                navigateUp()
            }

            addChildClickViewIds(R.id.tv_delete)

            setOnItemChildClickListener { adapter, view, position ->
                if (view.id == R.id.tv_delete) {

                    deleteDialog {

                        mViewModel.id = data[position].id
                        mViewModel.address_del()
                    }

                }


            }

        }


        btnCreate.setOnClickListener {
            navigateAddress(type = 0)
        }

    }

    private fun navigateAddress(type: Int, p: String = "", c: String = "", a: String = "") {
        navigate(R.id.addressFragment, Bundle().apply {
            putInt(AddressFragment.KEY_TYPE, type)
            putString(AddressFragment.PROVINCE, p)
            putString(AddressFragment.CITY, c)
            putString(AddressFragment.AREA, a)
        })
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observe(this) {
            addressAdapter.setNewInstance(it)
        }
        mViewModel.notify.observe(this) {
            mViewModel.requestData()
        }
        mViewModel.deleteNotify.observer(this) {
            addressAdapter.removeAt(addressAdapter.data.indexOfFirst { it.id == mViewModel.id })
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.requestData()
    }

}